//
//  KWMBasePageVC.m
//  iForeseaLife
//
//  Created by kevin on 7/7/14.
//  Copyright (c) 2014 Kollway Mobile. All rights reserved.
//

#import "KWMBasePageVC.h"
#import "MJRefresh.h"
#import "UIColor+SAMAdditions.h"

@implementation KWMBasePageVC{
    
    //view
    __weak UIView *weakEmptyView;
}

- (NSMutableArray *)dataList {
    if(_dataList == nil){
        _dataList = [[NSMutableArray alloc] initWithCapacity:100];
    }
    return _dataList;
}

/**
 * 设置下拉,上拉刷新
 */
- (void)setupPullToRefresh {
    __weak UITableView *tableView = [self targetTableView];
    __weak KWMBasePageVC *weakSelf = self;
    
    MJRefreshHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf reLoadData];
        });
    }];
    tableView.header = header;
    tableView.header.automaticallyChangeAlpha = YES;
    
    tableView.footer = [self createFooter];
}

- (MJRefreshAutoNormalFooter *)createFooter {
    __weak KWMBasePageVC *weakSelf = self;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
    footer.hidden = YES;
    return footer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tbv = [self targetTableView];
    tbv.delegate = self;
    tbv.dataSource = self;
    
    self.totalCount = @0;
    self.pageSize = @0;
    self.currentPage = @0;
    [self setupPullToRefresh];
    [self reLoadData];
}

- (void)loadData {
    NSAssert(NO, @"子类必须重写该方法");
}


- (UITableView *)targetTableView {
    NSAssert(NO, @"子类必须重写该方法");
    return nil;
}


- (void)reLoadData {
    self.currentPage = @0;
    self.totalCount = @0;
    [self loadData];
}

- (BOOL)isReload {
    _isReload = (self.totalCount.integerValue == 0);
    return _isReload;
}

- (BOOL)hasNextPage {
    _hasNextPage = self.totalCount.integerValue <= 0
                    || self.pageSize.integerValue * self.currentPage.integerValue < self.totalCount.integerValue;

    return _hasNextPage;
}

- (MBProgressHUD *)showLoading {
    
    //数据为空的时候显示Loading框
    if ([self.targetTableView respondsToSelector:@selector(numberOfSections)]) {
        NSInteger sectionCount = [self.targetTableView numberOfSections];
        for(NSInteger i = 0; i < sectionCount; i++){
            if([self.targetTableView numberOfRowsInSection:i] <= 0){
                return [super showLoading];
            }
        }
    }else{
        if([self.targetTableView numberOfRowsInSection:0] <= 0){
            return [super showLoading];
        }
    }
    return nil;
}

- (MBProgressHUD *)showAlertLoading {
    return [super showLoading];
}

- (void)appendDataList:(KWMRequestListResult *)result {
    if(self.isReload){
        self.isReload = NO;
        [self.dataList removeAllObjects];
    }
    
    self.currentPage = result.page;
    self.pageSize = result.pageSize;
    self.totalCount = result.totalCount;
    
    if (result.data != nil) {
        [self.dataList addObjectsFromArray:result.data];
    }
    
    __weak UITableView *tableView = [self targetTableView];
    if (![self hasNextPage]) {
        tableView.footer = nil;
    }else if (tableView.footer == nil){
        tableView.footer = [self createFooter];
    }
    [tableView reloadData];
    
    if (self.dataList.count <= 0) {
        [self showEmptyView];
    }else{
        [self hideEmptyView];
    }
}

- (void)hideLoading {
    [super hideLoading];
    
    __weak UITableView *tableView = [self targetTableView];
    [tableView.header endRefreshing];
    [tableView.footer endRefreshing];
}

-(void)showEmptyView{
    [self hideEmptyView];
    
//    UITableView *tbv = [self targetTableView];
//    KWMNoDataView *emptyView = [[KWMNoDataView alloc] initWithFrame:tbv.frame];
//    tbv.backgroundColor = [UIColor clearColor];
//
//    [self.view insertSubview:emptyView belowSubview:tbv];
//    weakEmptyView = emptyView;
}

-(void)hideEmptyView{
    if(weakEmptyView != nil){
        weakEmptyView.hidden = YES;
        [weakEmptyView removeFromSuperview];
        weakEmptyView = nil;
    }
}

#pragma mark - EGORefreshTableDelegate

- (void)egoRefreshTableDidTriggerRefresh:(EGORefreshPos)aRefreshPos {
    switch (aRefreshPos){
        case EGORefreshHeader:{
            [self reLoadData];
            break;
        }
        case EGORefreshFooter:{
            [self loadData];
            break;
        }
    }
}

- (BOOL)egoRefreshTableDataSourceIsLoading:(UIView *)view {
    return [self isLoading];
}

- (NSDate *)egoRefreshTableDataSourceLastUpdated:(UIView *)view {
    return [NSDate date];
}

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSAssert(NO, @"子类必须重写该方法");
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSAssert(NO, @"子类必须重写该方法");
    return nil;
}

@end
